﻿using System;
using System.Collections.Generic;
using Relocation.Com.Tools;
using Relocation.Base;
using Relocation.Data;
using Relocation.Com;



namespace Relocation.Forms.Window
{
	public partial class VillagesStatistics : Base.UI.BaseForm
	{
		/// <summary>
		/// 统计行
		/// </summary>
		VillageInfo VillageInfo_Total;

		

		#region 构造函数
		public VillagesStatistics():base()
		{
			InitializeComponent();
			this.villagesStatistics_VillagesDataGridView.init();

		}

		public VillagesStatistics(Session session)
			: base(session)
		{
			InitializeComponent();
			this.villagesStatistics_VillagesDataGridView.init();
		}
		#endregion

		private void VillagesStatistics_Load(object sender, EventArgs e)
		{
			this.projectsBindingSource.DataSource = this.Session.DataModel.Projects;
		}


		private List<VillageInfo> GetVillageInfo(Project projects)
		{
			List<VillageInfo> VillageInfoCollection = new List<VillageInfo>();
			VillageInfo_Total = new VillageInfo();
			VillageInfo_Total.VillageName = "总计：";

			foreach (Village village in projects.Villages)
			{
				village.Areas.Load();
				village.Compensates.Load();
				VillageInfo _VillageInfo = GetVillageInfoByVillage(village);
				VillageInfoCollection.Add(_VillageInfo);

				VillageInfo_Total.PoolCount += _VillageInfo.PoolCount;
				VillageInfo_Total.Pool_Mu += _VillageInfo.Pool_Mu;
				VillageInfo_Total.Pool_Centiare += _VillageInfo.Pool_Centiare;


				VillageInfo_Total.Farm_Mu += _VillageInfo.Farm_Mu;
				VillageInfo_Total.Farm_Centiare += _VillageInfo.Farm_Centiare;
				VillageInfo_Total.FarmCount += _VillageInfo.FarmCount;


				VillageInfo_Total.HomesteadCount += _VillageInfo.HomesteadCount;
				VillageInfo_Total.Homestead_Centiare += _VillageInfo.Homestead_Centiare;
				VillageInfo_Total.Homestead__Mu += _VillageInfo.Homestead__Mu;


				VillageInfo_Total.RoadCount += _VillageInfo.RoadCount;
				VillageInfo_Total.Road_Centiare += _VillageInfo.Road_Centiare;
				VillageInfo_Total.Road_Mu += _VillageInfo.Road_Mu;

				VillageInfo_Total.AreaTotal_Centiare += _VillageInfo.AreaTotal_Centiare;
				VillageInfo_Total.AreaTotal_Mu += _VillageInfo.AreaTotal_Mu;


				VillageInfo_Total.AuditMoney_Total += _VillageInfo.AuditMoney_Total;
				VillageInfo_Total.CompensateMoner_Total += _VillageInfo.CompensateMoner_Total;

			}
			VillageInfoCollection.Add(VillageInfo_Total);
			return VillageInfoCollection;
		}

		private VillageInfo GetVillageInfoByVillage(Village villages)
		{
			VillageInfo _VillageInfo = new VillageInfo();
			_VillageInfo.VillageName = villages.name;
			_VillageInfo.Principal = villages.principal;

			int PoolCount = 0;
			decimal Pool_Centiare = 0;
			decimal Pool_Mu = 0;
			int FarmCount = 0;
			decimal Farm_Centiare = 0;
			decimal Farm_Mu = 0;
			int HomesteadCount = 0;
			decimal Homestead_Centiare = 0;
			decimal Homestead__Mu = 0;
			int RoadCount = 0;
			decimal Road_Centiare = 0;
			decimal Road_Mu = 0;
			int CountTotal = 0;
			decimal AreaTotal_Centiare = 0;
			decimal AreaTotal_Mu = 0;
			decimal CompensateMoner_Total = 0;
			decimal AuditMoney_Total = 0;

			foreach (Area area in villages.Areas)
			{
				string name = area.area_name;
				decimal Centiare = area.centiare == null ? 0 : (decimal)area.centiare;
				decimal Mu = area.mu == null ? 0 : (decimal)area.mu;

				if (name.Equals("塘沟"))
				{
					PoolCount++;
					Pool_Centiare += Centiare;
					Pool_Mu += Mu;
				} else if (name.Equals("农田"))
				{
					FarmCount++;
					Farm_Centiare += Centiare;
					Farm_Mu += Mu;
				} else if (name.Equals("宅基地"))
				{
					HomesteadCount++;
					Homestead_Centiare += Centiare;
					Homestead__Mu += Mu;
				} else if (name.Equals("道路"))
				{
					RoadCount++;
					Road_Centiare += Centiare;
					Road_Mu += Mu;
				}
			}
			AreaTotal_Centiare = Pool_Centiare + Farm_Centiare + Homestead_Centiare + Road_Centiare;
			AreaTotal_Mu = Pool_Mu + Farm_Mu + Homestead__Mu + Road_Mu;
			foreach (Compensate ccompensate in villages.Compensates)
			{
				CompensateMoner_Total += ccompensate.compensate_moner == null ? 0 : (decimal)ccompensate.compensate_moner;
				AuditMoney_Total += ccompensate.audit_money == null ? 0 : (decimal)ccompensate.audit_money;
			}
			_VillageInfo.PoolCount = PoolCount;
			_VillageInfo.Pool_Centiare = Pool_Centiare;
			_VillageInfo.Pool_Mu = Pool_Mu;
			_VillageInfo.FarmCount = FarmCount;
			_VillageInfo.Farm_Centiare = Farm_Centiare;
			_VillageInfo.Farm_Mu = Farm_Mu;
			_VillageInfo.HomesteadCount = HomesteadCount;
			_VillageInfo.Homestead_Centiare = Homestead_Centiare;
			_VillageInfo.Homestead__Mu = Homestead__Mu;
			_VillageInfo.RoadCount = RoadCount;
			_VillageInfo.Road_Centiare = Road_Centiare;
			_VillageInfo.Road_Mu = Road_Mu;
			_VillageInfo.CountTotal = CountTotal;
			_VillageInfo.AreaTotal_Centiare = AreaTotal_Centiare;
			_VillageInfo.AreaTotal_Mu = AreaTotal_Mu;
			_VillageInfo.CompensateMoner_Total = CompensateMoner_Total;
			_VillageInfo.AuditMoney_Total = AuditMoney_Total;
			return _VillageInfo;


		}

		private void ShowAreaTotal(VillageInfo villageInfo)
		{
			dataGridView1.Rows.Clear();
			string[] PoolTotal = new string[] { "塘沟", villageInfo.PoolCount.ToString(), villageInfo.Pool_Centiare.ToString(), villageInfo.Pool_Mu.ToString() };
			string[] FarmTotal = new string[] { "农田", villageInfo.FarmCount.ToString(), villageInfo.Farm_Centiare.ToString(), villageInfo.Farm_Mu.ToString() };
			string[] HomesteadTotal = new string[] { "宅基地", villageInfo.HomesteadCount.ToString(), villageInfo.Homestead_Centiare.ToString(), villageInfo.Homestead__Mu.ToString() };
			string[] RoadTotal = new string[] { "道路", villageInfo.RoadCount.ToString(), villageInfo.Road_Centiare.ToString(), villageInfo.Road_Mu.ToString() };
			dataGridView1.Rows.Add(PoolTotal);
			dataGridView1.Rows.Add(FarmTotal);
			dataGridView1.Rows.Add(HomesteadTotal);
			dataGridView1.Rows.Add(RoadTotal);
		}

		private void ShowCompensateTotal(VillageInfo villageInfo)
		{
			this.TextBoxCompensateMoner.Text = villageInfo.CompensateMoner_Total.ToString();
			this.TextBoxAuditMoney.Text = villageInfo.AuditMoney_Total.ToString();
		}



		/// <summary>
		/// 单位信息结构
		/// </summary>
		public struct VillageInfo
		{
			/// <summary>
			/// 单位名称
			/// </summary>
			private string _VillageName;

			/// <summary>
			/// 单位名称
			/// </summary>
			public string VillageName
			{
				get
				{
					return _VillageName;
				}
				set
				{
					_VillageName = value;
				}
			}

			/// <summary>
			/// 负责人
			/// </summary>
			private string _Principal;

			/// <summary>
			/// 负责人
			/// </summary>
			public string Principal
			{
				get
				{
					return _Principal;
				}
				set
				{
					_Principal = value;
				}
			}

			/// <summary>
			/// 塘沟数量
			/// </summary>
			private int _PoolCount;

			/// <summary>
			/// 塘沟数量
			/// </summary>
			public int PoolCount
			{
				get
				{
					return _PoolCount;
				}
				set
				{
					_PoolCount = value;
				}
			}

			/// <summary>
			/// 塘沟面积（平方米）
			/// </summary>
			private decimal _Pool_Centiare;

			/// <summary>
			/// 塘沟面积（平方米）
			/// </summary>
			public decimal Pool_Centiare
			{
				get
				{
					return _Pool_Centiare;
				}
				set
				{
					_Pool_Centiare = value;
				}
			}

			/// <summary>
			/// 塘沟面积（亩）
			/// </summary>
			private decimal _Pool_Mu;

			/// <summary>
			/// 塘沟面积（亩）
			/// </summary>
			public decimal Pool_Mu
			{
				get
				{
					return _Pool_Mu;
				}
				set
				{
					_Pool_Mu = value;
				}
			}

			/// <summary>
			/// 农田数量
			/// </summary>
			private int _FarmCount;

			/// <summary>
			/// 农田数量
			/// </summary>
			public int FarmCount
			{
				get
				{
					return _FarmCount;
				}
				set
				{
					_FarmCount = value;
				}
			}

			/// <summary>
			/// 农田面积（平方米）
			/// </summary>
			private decimal _Farm_Centiare;

			/// <summary>
			/// 农田面积（平方米）
			/// </summary>
			public decimal Farm_Centiare
			{
				get
				{
					return _Farm_Centiare;
				}
				set
				{
					_Farm_Centiare = value;
				}
			}

			/// <summary>
			/// 农田面积（亩）
			/// </summary>
			private decimal _Farm_Mu;

			/// <summary>
			/// 农田面积（亩）
			/// </summary>
			public decimal Farm_Mu
			{
				get
				{
					return _Farm_Mu;
				}
				set
				{
					_Farm_Mu = value;
				}
			}

			/// <summary>
			/// 宅基地数量
			/// </summary>
			private int _HomesteadCount;

			/// <summary>
			/// 宅基地数量
			/// </summary>
			public int HomesteadCount
			{
				get
				{
					return _HomesteadCount;
				}
				set
				{
					_HomesteadCount = value;
				}
			}

			/// <summary>
			/// 宅基地面积（平方米）
			/// </summary>
			private decimal _Homestead_Centiare;

			/// <summary>
			/// 宅基地面积（平方米）
			/// </summary>
			public decimal Homestead_Centiare
			{
				get
				{
					return _Homestead_Centiare;
				}
				set
				{
					_Homestead_Centiare = value;
				}
			}

			/// <summary>
			/// 宅基地面积（亩）
			/// </summary>
			private decimal _Homestead__Mu;

			/// <summary>
			/// 宅基地面积（亩）
			/// </summary>
			public decimal Homestead__Mu
			{
				get
				{
					return _Homestead__Mu;
				}
				set
				{
					_Homestead__Mu = value;
				}
			}

			/// <summary>
			/// 道路数量
			/// </summary>
			private int _RoadCount;

			/// <summary>
			/// 道路数量
			/// </summary>
			public int RoadCount
			{
				get
				{
					return _RoadCount;
				}
				set
				{
					_RoadCount = value;
				}
			}

			/// <summary>
			/// 道路面积（平方米）
			/// </summary>
			private decimal _Road_Centiare;

			/// <summary>
			/// 道路面积（平方米）
			/// </summary>
			public decimal Road_Centiare
			{
				get
				{
					return _Road_Centiare;
				}
				set
				{
					_Road_Centiare = value;
				}
			}

			/// <summary>
			/// 道路面积（亩）
			/// </summary>
			private decimal _Road_Mu;

			/// <summary>
			/// 道路面积（亩）
			/// </summary>
			public decimal Road_Mu
			{
				get
				{
					return _Road_Mu;
				}
				set
				{
					_Road_Mu = value;
				}
			}

			/// <summary>
			/// 数量小计
			/// </summary>
			private int _CountTotal;

			/// <summary>
			/// 数量小计
			/// </summary>
			public int CountTotal
			{
				get
				{
					return _CountTotal;
				}
				set
				{
					_CountTotal = value;
				}
			}

			/// <summary>
			/// 面积小计（平方米）
			/// </summary>
			private decimal _AreaTotal_Centiare;

			/// <summary>
			/// 面积小计（平方米）
			/// </summary>
			public decimal AreaTotal_Centiare
			{
				get
				{
					return _AreaTotal_Centiare;
				}
				set
				{
					_AreaTotal_Centiare = value;
				}
			}

			/// <summary>
			/// 面积小计（亩）
			/// </summary>
			private decimal _AreaTotal_Mu;

			/// <summary>
			/// 面积小计（亩）
			/// </summary>
			public decimal AreaTotal_Mu
			{
				get
				{
					return _AreaTotal_Mu;
				}
				set
				{
					_AreaTotal_Mu = value;
				}
			}

			/// <summary>
			/// 补偿金额小计
			/// </summary>
			private decimal _CompensateMoner_Total;

			/// <summary>
			/// 补偿金额小计
			/// </summary>
			public decimal CompensateMoner_Total
			{
				get
				{
					return _CompensateMoner_Total;
				}
				set
				{
					_CompensateMoner_Total = value;
				}
			}


			private decimal _AuditMoney_Total;

			public decimal AuditMoney_Total
			{
				get
				{
					return _AuditMoney_Total;
				}
				set
				{
					_AuditMoney_Total = value;
				}
			}

		}

		private void button2_Click(object sender, EventArgs e)
		{
			try
			{
				Project pro = this.Session.DataModel.getProjectById((int)this.comboBox1.SelectedValue);
				pro.Villages.Load();
				this.villagesStatistics_VillagesBindingSource.DataSource = GetVillageInfo(pro);
				ShowAreaTotal(this.VillageInfo_Total);
				ShowCompensateTotal(this.VillageInfo_Total);
				this.TextBoxVillagesCount.Text = pro.Villages.Count.ToString();
			} catch (Exception ex)
			{
				Log.Error(ex);
				MyMessagebox.Error(ex.Message);
			}
		}





	}
}
